package main;

import javax.swing.*;
import storage.*;
import java.awt.*;
import java.awt.event.*;

public class RenameDatabaseDialog extends JDialog
{	
	private JButton saveButton = new JButton("Save");
	private JButton cancelButton = new JButton("Cancel");
	private JLabel dbLabel = new JLabel("Database Name :");
	private JTextField dbnameInput = new JTextField(10);
	private JPanel pane = new JPanel();
	private DataAccessManager data;
	private ManagementFrame parentFrame;
	private String oldName;
	
	Font fon = new Font("Dialog",Font.PLAIN,15);
	
	private String usr = "";
	
	
	void buildConstraints(GridBagConstraints gbc,int gx, int gy, int gw, int gh, int wx, int wy)
	{
		gbc.gridx = gx;			
		gbc.gridy = gy;			
		gbc.gridwidth = gw;		
		gbc.gridheight = gh;	
		gbc.weightx = wx;		
		gbc.weighty = wy;		
	}
	
	
	private void setMyPane()
	{
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		pane.setLayout(gridbag);
		
		buildConstraints(constraints,0,0,3,1,1,1);
		
		buildConstraints(constraints,3,0,3,1,1,1);
		gridbag.setConstraints(dbLabel, constraints);
		GridBagConstraints gbc_dbLabel = new GridBagConstraints();
		gbc_dbLabel.insets = new Insets(0, 0, 5, 5);
		gbc_dbLabel.gridx = 0;
		gbc_dbLabel.gridy = 0;
		pane.add(dbLabel, gbc_dbLabel);

		buildConstraints(constraints,0,2,2,1,1,1);

		buildConstraints(constraints,2,2,2,1,1,1);

		setLocation(200,80);
		setContentPane(pane);
		gridbag.setConstraints(dbnameInput, constraints);
		dbnameInput.setFont(fon);
		GridBagConstraints gbc_dbnameInput = new GridBagConstraints();
		gbc_dbnameInput.insets = new Insets(0, 0, 5, 0);
		gbc_dbnameInput.gridy = 0;
		gbc_dbnameInput.gridx = 1;
		pane.add(dbnameInput, gbc_dbnameInput);
		gridbag.setConstraints(saveButton, constraints);
		GridBagConstraints gbc_createButton = new GridBagConstraints();
		gbc_createButton.insets = new Insets(0, 0, 5, 5);
		gbc_createButton.gridx = 0;
		gbc_createButton.gridy = 2;
		pane.add(saveButton, gbc_createButton);
		gridbag.setConstraints(cancelButton, constraints);
		GridBagConstraints gbc_cancelButton = new GridBagConstraints();
		gbc_cancelButton.insets = new Insets(0, 0, 5, 0);
		gbc_cancelButton.gridx = 1;
		gbc_cancelButton.gridy = 2;
		pane.add(cancelButton, gbc_cancelButton);
	}
	

	private void initListener()
	{
		ActListener actListener = new ActListener();
		saveButton.addActionListener(actListener);
		cancelButton.addActionListener(actListener);
	}
	
	class ActListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource() == saveButton)
				saveRun();
			if(e.getSource() == cancelButton)
				cancelRun();
		}
	}
	
	private void cancelRun()
	{
		this.dispose();
	}
	
	private void saveRun()
	{
		String databaseName = dbnameInput.getText();
		if(databaseName.equals(""))
		{
			JOptionPane.showMessageDialog(null,"Please enter your database name.","Error" ,JOptionPane.ERROR_MESSAGE);
			return;
		}
		else
		{
			if (data.renameDatabase(oldName, databaseName))
			{
				parentFrame.refreshDatabaseTree();
				this.dispose();
			}
			else
			{
				JOptionPane.showMessageDialog(null,"Database name exists.Please enter another name.","Error" ,JOptionPane.ERROR_MESSAGE);
				return;
			}
		}
	}
	public RenameDatabaseDialog(String oldName,ManagementFrame frame)
	{
		data = new DataAccessManager();
		parentFrame = frame;
		this.oldName = oldName;
		setTitle("Rename Database");
		setModal(true);
		dbnameInput.setText(oldName);
		setSize(433,178);
		initListener();
		setMyPane();
		setVisible(true);
	}
}
